<?php
	class City_model extends MY_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		public function get_city($id=FALSE, $c_id=FALSE){
			$this->load->helper('string');
			if($id === FALSE && $c_id === FALSE){
				$this->db->select('ci.*, c.name');
				$this->db->from('city ci');
				$this->db->join('country c', 'c.iso = ci.country_code', 'left');
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get();				
				return $query->result_array();
			}
			elseif($id===FALSE && $c_id != FALSE){			
				$this->db->select('ci.*, c.name');
				$this->db->from('city ci');
				$this->db->join('country c', 'c.iso = ci.country_code', 'left');
				$this->db->where('ci.country_code = "'.$c_id.'"', NULL, FALSE);
				$this->db->limit($this->record_per_page, $this->limit);
				
				$query = $this->db->get();
				return $query->result_array();
			}
			else{
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get_where('city', array('city.id' => $id));
				return $query->row_array();
			}
		}		
		
		public function get_city_list($c_id=FALSE){
			if($c_id!=FALSE)
				$query = $this->db->get_where('city', array('country_code' => "$c_id"));
			else
				$query = $this->db->get('city');
			return $query->result_array();
		}
		
		public function get_total(){
			return count($this->get_city_list());
		}
		
		public function get_total_search($src=FALSE){
			$this->db->select('ci.*, c.name');
			$this->db->from('city ci');
			$this->db->join('country c', 'c.iso = ci.country_code', 'left');
			$this->db->like('city', $src);				
			$query = $this->db->get();				
			return $query->num_rows();
		}
		
		public function get_total_filter($c_id=FALSE){
			$this->db->select('ci.*, c.name');
			$this->db->from('city ci');
			$this->db->join('country c', 'c.iso = ci.country_code', 'left');
			$this->db->where('ci.country_code = "'. $c_id .'"', NULL, FALSE);			
			$query = $this->db->get();
			return $query->num_rows();
		}
		
		public function search($src=FALSE){
			$this->db->select('ci.*, c.name');
			$this->db->from('city ci');
			$this->db->join('country c', 'c.iso = ci.country_code', 'left');
			$this->db->like('city', $src);			
			$this->db->limit($this->record_per_page, $this->limit);
			$query = $this->db->get();				
			return $query->result_array();
		}
		
		public function save(){
			$id 	= $this->input->post('id');
			
			$data = array(
				"city" 			=> $this->input->post('city'),	
				"country_code"	=> $this->input->post('country_code')
			);
			if(empty($id))
				return $this->db->insert('city', $data);
			else{
				$this->db->where('id', $id);
				return $this->db->update('city', $data);
			}
		}
		
		public function delete($id){
			$sql = "SELECT `id` FROM `district` WHERE `city_id` = $id";
			$query = $this->db->query($sql);
			if($query->num_rows()==0){
				$this->db->where('id', $id);
				$this->db->delete('city');
			}
		}		
	}
?>